Here is a Python implementation of the bubble sort algorithm:
```
def bubble_sort(arr):
    n = len(arr)
    for i in range(n-1):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr
```
Here's an explanation of how the algorithm works:

1. The outer loop iterates `n-1` times, where `n` is the length of the input array.
2. The inner loop iterates from the first element to the second-to-last element of the array (i.e., `n-i-1` elements).
3. For each pair of adjacent elements, we check if the first element is greater than the second element. If it is, we swap them.
4. After the inner loop finishes, the largest element in the array has "bubbled" up to the end of the array, so we don't need to consider it again in subsequent iterations.
5. We repeat the process until the outer loop finishes, at which point the entire array is sorted.

Here's an example usage:
```
arr = [5, 2, 8, 3, 1, 6, 4]
arr = bubble_sort(arr)
print(arr)  # [1, 2, 3, 4, 5, 6, 8]
```
Note that bubble sort has a time complexity of O(n^2), which makes it less efficient than other sorting algorithms like quicksort or mergesort for large datasets. However, it can still be useful for small datasets or for educational purposes.